<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mgifts extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function getListGifts()
    {
        $sql = "SELECT * FROM vn_gift";
        $data = $this->db->query($sql)->result_object();
        return $data;
    }
    
    public function addgiftsDb($name,$note)
    {
        $data = array(
            'name' => $name
        );
        if(!in_array($note, array(null,'')))
        {
            $data['note'] = $note;
        }
        $count = $this->db->query("SELECT name FROM vn_gift WHERE LOWER(name) = '".strtolower($name)."'")->num_rows();
        
        if( $count == 0){
            $this->db->insert('vn_gift', $data);
            return 200;
        }
        return 201;
    }
    
    public function getgiftsDb($id)
    {
        $query = $this->db->query("SELECT * FROM vn_gift WHERE id = ".$id);
        if($query->num_rows() > 0)
        {
            $data = $query->row_object();
            return $data;
        }else{
            redirect('manage/gifts/index'); 
        }
    }
    
    public function editgiftsDb($name,$note,$id)
    {
        $data = array(
            'name' => $name
        );
        if(!in_array($note, array(null,'')))
        {
            $data['note'] = $note;
        }
        
        $count = $this->db->query("SELECT name FROM vn_gift WHERE LOWER(name) = '".strtolower($name)."' AND id != ".$id)->num_rows();
        
        if( $count == 0){
            $this->db->update('vn_gift', $data, "id = ".$id);
            return 200;
        }
        return 201;
    }
    
    public function deletegiftsDb($id)
    {
        $this->db->delete('vn_gift', array('id' => $id));
        return 200;
    }
}